package com.dy.org.controller;

import com.dy.basic.annotation.PreAuthorize;
import com.dy.basic.util.AjaxResult;
import com.dy.basic.util.PageList;
import com.dy.org.domain.Employee;
import com.dy.org.query.EmployeeQuery;
import com.dy.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有的信息
     */
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 根据id查询用户的信息
     */
/*    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }*/

    /**
     * 修改或者添加
     */
    @PutMapping
    public AjaxResult UpOrAdd(@RequestBody Employee employee){
            if (employee.getId() == null){
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除角色",sn = "emp:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
            employeeService.delete(id);
            return AjaxResult.me().success();
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
            employeeService.deleteAll(ids);
            return AjaxResult.me().success();
    }

    /*
     * 分页查询
    */
    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

}
